using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace VariationsEnabledContentByQueryWebPart.WebPartCode
{
    public class VariationsEnabledContentByQueryEditorPart : System.Web.UI.WebControls.WebParts.EditorPart
    {

        private CheckBox cbVariationsEnabled = null;

        protected override void CreateChildControls()
        {
            VariationsEnabledContentByQueryWebPart webpart = WebPartToEdit as VariationsEnabledContentByQueryWebPart;

            cbVariationsEnabled = new CheckBox();
            cbVariationsEnabled.Text = "Variations enabled";
            this.Controls.Add(cbVariationsEnabled);

            Table variationtable = new Table();

            TableHeaderRow headerrow = new TableHeaderRow();

            TableHeaderCell labeltitleheadercell = new TableHeaderCell();
            labeltitleheadercell.Text = "Title";
            headerrow.Cells.Add(labeltitleheadercell);

            TableHeaderCell labelvariationurlheadercell = new TableHeaderCell();
            labelvariationurlheadercell.Text = "Variation Url";
            headerrow.Cells.Add(labelvariationurlheadercell);

            variationtable.Rows.Add(headerrow);

            foreach (Microsoft.SharePoint.Publishing.VariationLabel ual in Microsoft.SharePoint.Publishing.Variations.Current.UserAccessibleLabels)
            {
                TableRow labelrow = new TableRow();

                TableCell labeltitlecell = new TableCell();
                labeltitlecell.Text = ual.DisplayName;
                labelrow.Cells.Add(labeltitlecell);

                TableCell labelvariationurlcell = new TableCell();
                labelvariationurlcell.Text = webpart.GetVariationLabelWebUrl(ual);
                labelrow.Cells.Add(labelvariationurlcell);

                variationtable.Rows.Add(labelrow);
            }
            this.Controls.Add(variationtable);

            base.CreateChildControls();
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();

            VariationsEnabledContentByQueryWebPart webpart = WebPartToEdit as VariationsEnabledContentByQueryWebPart;
            if (webpart != null)
            {
                webpart.VariationsEnabled = cbVariationsEnabled.Checked;
            }
            return true;
        }

        public override void SyncChanges()
        {
            EnsureChildControls();

            VariationsEnabledContentByQueryWebPart webpart = WebPartToEdit as VariationsEnabledContentByQueryWebPart;
            if (webpart != null)
            {
                cbVariationsEnabled.Checked = webpart.VariationsEnabled;
            }
        }

    }
}
